Uec3.InventoryWindow = Ext.extend(Ext.UecWindow, {
	constructor:function(config){
		this.ajax=Uec3.App.Uec3Ajax;
		/*var inventory_storeLevel1 = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({url : Uec3.App.Constants.homeUrl+ '/part/category_getFirstCategorys.do'}),
			reader : new Ext.data.JsonReader({root : 'root'}, [{name : 'id'}, {name : 'level1Name'}])
		});
		this.formLevel1Cobmo = new Ext.form.ComboBox({
			name : 'level1Id',store : inventory_storeLevel1,selectOnFocus : true,forceSelection : true,
			emptyText : '请选择一级分类',width : 150,mode : 'local',triggerAction : 'all',valueField : 'id',hiddenName:'level1Id',
			displayField : 'level1Name',loadingText : '加载中...',editable : false
		});
		var inventory_storeLevel2 = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({url : Uec3.App.Constants.homeUrl+ '/part/category_getSubCategoryList.do'}),
			reader : new Ext.data.JsonReader({root : 'root',fields : [{name : 'id'}, {name : 'level2Name'}]})
		});			
					
		this.formLevel2Cobmo = new Ext.form.ComboBox({
			name : 'level2Name',selectOnFocus : true,forceSelection : false,width : 150,
			store : inventory_storeLevel2,emptyText : '请选择二级分类',mode : 'local',triggerAction : 'all',
			valueField : 'level2Name',displayField : 'level2Name',loadingText : '加载中...',editable : false
		});	
		*/		
		var inventory_store_ds = new Ext.data.JsonStore({
				proxy : new Ext.data.HttpProxy({url : Uec3.App.Constants.homeUrl+ '/store/base!query.do'}),
				reader : new Ext.data.JsonReader({root:'root',totalProperty: "totalProperty"},
					[{name : 'id',type:'int'},{name : 'name',type:'string'},{name : 'pyCode',type:'string'}])
			});	
		this.formStoreCobmo = new Ext.form.LookupBox2({
			fieldLabel : '仓库',
	        store:inventory_store_ds ,
	        displayField:'name',
	        lookupField:'pyCode',
	        valueField:'id',
	        hiddenName:"storeId",
	        typeAhead: false,
	        mode: 'remote',
	        width:150,
	        triggerAction: 'all',
	        emptyText:'请选择仓库...',
	        selectOnFocus:true, 			
	        queryDelay:10,
	        typeAheadDelay:10,
	        pageSize:5,
	        autoDestory:true,
	        queryParam:'keyword',
	        app:Uec3.App,
	     	triggerTags:[{moduleId: 'store_list',triggerClass:'x-form-search-trigger'},{moduleId: 'store_add'}]
		   });			
					
		/*var ds_code_select = new Ext.data.JsonStore({
	    	 	url:Uec3.App.Constants.homeUrl+"/part/queryPart!getPartByCodeImplicitExt.do",
	     		root:'root',
	     		totalProperty:'totalProperty',
        		fields:[{name:'code',type:'string'},{name:'name',type:'string'}]
	 	});	*/		
					
					
		this.publicanCombo_grid=Uec3.App.dictionary.getCombo("partproperty_publicanLevel",{hiddenName:'publicanLevel'});
		this.searchButton=new Ext.Button({
			width:80,
			text:'查  询',
			style:'margin:20px 0px 0px 80px;',
			iconCls:'icon-search',
			handler:this.funSearch.createDelegate(this)});
		var sourceCombo=Uec3.App.dictionary.getCombo("part_source",{name:"source",hiddenName : 'source'});	
		this.formPanel=new Ext.FormPanel({
			width : 200,
			border:false,
	      	style:'padding-left:20px',
	      	defaults:{hideLabel:true},
	      	items:[
	      	  {xtype:'checkbox',name:'storeIdCheckbox',boxLabel:'仓库',margins:'0 0 5 20'},
	      	  this.formStoreCobmo,
              {xtype:'checkbox',name:'shelfCheckbox',boxLabel:'仓位',margins:'0 0 5 0'},
              {xtype:"textfield",name:"shelf",width:150},
              {xtype:'checkbox',name:'codeCheckbox',boxLabel:'编码',margins:'0 0 5 0'},
              {xtype:"textfield",name:"code",width:150},
			  {xtype:'checkbox',name:'nameCheckbox',boxLabel:'零件名称',margins:'0 0 5 0'},
              {xtype:"textfield",name:"name",width:150},
			  {xtype:'checkbox',name:'sourceCheckbox',boxLabel:'来源',margins:'0 0 5 0'},
              sourceCombo,
              {xtype:'checkbox',name:'brandCheckbox',boxLabel:'品牌',margins:'0 0 5 0'},
	      	  {xtype:"textfield",width:150,name:'brand'},
	      	  {xtype:'checkbox',name:'carModelCheckbox',boxLabel:'车型',margins:'0 0 5 0'},
	      	  {xtype:"textfield",width:150,name:'carModel'},
	      	  {xtype:'checkbox',name:'originCheckbox',boxLabel:'产地',margins:'0 0 5 0'},
	      	  {xtype:"textfield",width:150,name:'origin'},
	      	  this.searchButton
	      	]
		});
		this.westPanel = new Ext.Panel({
			title : '模糊查询',
			region : 'west',
			split : true,
			width : 200,
			minSize : 200,
			maxSize : 200,
			collapsible : true,
			lines : false,
			layout:'fit',
			margins : '3 0 3 3',
			cmargins : '3 3 3 3',
			items : [this.formPanel]
		});
		
		var inventory_ds = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
						url : Uec3.App.Constants.homeUrl+'/inventory/browse_inventory!extList.do'
					}),
			baseParams : {
				//partId:0,
				start : 0,
				limit : 10
			},
			reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, 
				[{name : 'id'},{name : 'companyId'}, {name : 'storeId'},{name : 'shelf'},{name : 'partId'}, {name : 'code'}, 
				 {name : 'source'}, {name : 'name'}, {name : 'pyCode'}, {name : 'measurementUnit'}, {name : 'partCategoryId'}, 
				 {name : 'level1Name'}, {name : 'level2Name'}, {name : 'level1Id'}, {name : 'durability'},{name:'shelf'},
				 {name : 'brand'}, {name : 'origin'}, {name : 'cost'}, {name : 'retailPrice'}, {name : 'insurancePrice'}, 
				 {name : 'minPrice'}, {name : 'initQuantity'}, {name : 'cost'}, {name : 'onHand'}, {name : 'publicanLevel'},
				 {name : 'gmtEntry',type : 'string'}, {id:"gmtModify",name : 'gmtModify',type : 'string'}, {name : 'documentType'}, 
				 {name : 'documentId'}, {name : 'documentCode'}, {name : 'partnerId'}, {name : 'inventoryIdBySeller'}, 
				 {name : 'partnerName'}, {name : 'gmtLastQuotation'}, {name : 'gmtLastSold'}, {name : 'carModel'},
				 {name:'storeName'},{name:'sourceName'},{name:'documentTypeName'}]),
				remoteSort : true,
			// 默认排序字段
				sortInfo : {field : "id",direction : "asc"}
		});
		this.inventory_colModel = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
          	{header : "零件名称",width : 180,dataIndex : 'name'},
			{header : "零件编码",width : 120,sortable : true,dataIndex : 'code'}, 
			{header : "零件来源",width : 100,dataIndex : 'sourceName'},
			{header : "品牌",width : 80,dataIndex : 'brand'},
			{header : "车型",width : 80,dataIndex : 'carModel'}, 
			{
				header : "产地",
				width : 80,
				dataIndex : 'origin'
			}, {
				header : "供应商名称",
				width : 80,
				dataIndex : 'partnerName'
			},
			{
				header : "仓库名称",
				width : 80,
				dataIndex : 'storeName'
			},
			{
				header : "仓位",
				width : 80,
				dataIndex : 'shelf'
			}, {
				header : "初始数量",
				width : 80,
				dataIndex : 'initQuantity'

			}, {
				header : "剩余数量",
				width : 80,
				dataIndex : 'onHand'

			}, {
				header : "单据名称",
				width : 80,
				dataIndex : 'documentTypeName'
			}, {
				header : "单据号",
				width : 80,
				dataIndex : 'documentCode'
			}, {
				header : "入仓时间",
				width : 120,
				dataIndex : 'gmtEntry'
			}, {
				header : "修改时间",
				width : 120,
				dataIndex : 'gmtModify',
				id : 'gmtModify',
				align : 'center'

			}

	]);
					
	this.inventory_grid = new Ext.grid.GridPanel(
		{
			region : 'center',
			autoScroll : true,
			iconCls : 'icon-grid',
			enableColumnMove : false,
			enableColumnMoveResize : false,
			stripeRows : true,
			autoExpandColumn : 'gmtModify',
			height : 300,
			// autoExpandColumn:'gmtModify',
			loadMask : {
				msg : "...加载数据中"
			},
			store : inventory_ds,
			cm : this.inventory_colModel,
			sm : new Ext.grid.RowSelectionModel({
				singleSelect : true
			}),

			viewConfig : {
				// forceFit :true,
				columnsText : '显示的列',
				scrollOffset : 20,
				sortAscText : '升序',
				sortDescText : '降序'
			},
			bbar : new Ext.PagingToolbar({
				pageSize : 10,
				store : inventory_ds,
				display : true,
				displayInfo : true,
				displayMsg : '第 {0} - {1} 条  共 {2} 条',
				emptyMsg : "没有记录",
				items : [/**
							 * '-', { pressed: true,
							 * enableToggle:true, text:
							 * 'Show Preview', cls:
							 * 'x-btn-text-icon details',
							 * toggleHandler: function(btn,
							 * pressed){ var view =
							 * inventory_grid.getView();
							 * view.showPreview = pressed;
							 * view.refresh(); } }
							 */
				]
			})
		});
		
		
		
		this.partProperty_form=new Ext.TabPanel({
			//anchor:'100% 70%',
			region:"south",
			height:150,
			activeTab: 0,
			items:[
			   {
			   	title : '零件属性',
				iconCls: 'iconn-tab-receiveItem',
				layout:"fit",
				itemId:'partProperty_tab',
				items : [this.funGetTabPartPropertyForm.createDelegate(this)()]
			   },{
			   	title : '库存信息',
				iconCls: 'iconn-tab-receiveItem',
				layout:"fit",
				itemId:'inventory_tab',
				items : [this.funGetTabInventoryForm.createDelegate(this)()]
			   },{
			   	title : '报价信息',
				iconCls: 'iconn-tab-receiveItem',
				layout:"fit",
				itemId:'quotation_tab',
				items : [this.funGetTabQuotationForm.createDelegate(this)()]
			   }]
		});
		
		
		this.centerPanel=new Ext.Panel({
			region : 'center',
			activeTab : 0,
			layout:'border',
			defaults: {
			    //collapsible: true
				bodyStyle: 'padding:2px',
			   split: true
			},
			margins : '3 3 3 0',
			items:[this.inventory_grid,this.partProperty_form]
		});			
		config=this.getInitialConfig(config);
		Uec3.InventoryWindow.superclass.constructor.call(this,config);
	},
	getInitialConfig:function(config){
		if(!config.params.showType){
			config=Ext.apply(config||{},{items:[this.centerPanel,this.westPanel]});
		}else if(config.params.showType==1){
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("name"),true);
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("code"),true);
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("sourceName"),true);
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("brand"),true);
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("carModel"),true);
			this.inventory_colModel.setHidden(this.inventory_colModel.findColumnIndex("origin"),true);
			config.height=400;
			config.width=600;
			config=Ext.apply(config||{},{items:[this.centerPanel]});
		}
		return config;
	},
	funSearch:function(){
		var obj=this.formPanel.getForm().getValues();
		var checkboxs={};
		for(pro in obj){
			if(pro.indexOf("Checkbox")!=-1){
				var proObj=pro.substr(0,pro.length-8);
				checkboxs[proObj]=obj[proObj];
			}
		}
		this.inventory_grid.getStore().baseParams.json=Ext.encode(checkboxs);
		this.inventory_grid.getStore().load();
	},
	initEvents:function(){
		var _this=this;
		//this.formLevel1Cobmo.getStore().load();
		//this.formLevel1Cobmo.on("select",_this.funFormLevel1CobmoSelectEvent.createDelegate(_this));
		if(!this.params.showType){
			this.inventory_grid.getSelectionModel().on('rowselect',_this.funInventoryGridRowSelect.createDelegate(this));
		}
		this.on("show",this.funWindowAfterShow,this,{single:true});
		
		Uec3.InventoryWindow.superclass.initEvents.call(this);
	},
	funWindowAfterShow:function(){
		if(!this.params.showType){
		}else if(this.params.showType){
			if(this.params.partId){
				var record={};
				record.data={};
				record.data.partId=this.params.partId;
				this.funInventoryGridRowSelect(null,null,record);
				this.inventory_grid.getStore().baseParams.partId=this.params.partId;
			}
		}
		
		this.tab_partProperty_form.form.items.each(function(field){
			field.setReadOnly(true);
			//field.getEl().setStyle('background','#dddddd');
		});
		this.tab_inventory_form.form.items.each(function(field){
			field.setReadOnly(true);
		});
		this.tab_quotation_form.form.items.each(function(field){
			field.setReadOnly(true);
		});
		
		this.inventory_grid.getStore().load();
	},
	funGetTabPartPropertyForm:function(){
		var sourceCombo=Uec3.App.dictionary.getCombo("part_source",
			{hiddenName : 'source', 
			fieldLabel:'来源',
			mode : 'local',
			focusOrder:12});
		sourceCombo.getStore().load();
		this.tab_partProperty_form=new Ext.FormPanel({
			frame:true,
    		border:false,
    		height:00,
    		labelAlign:'right',
    		labelWidth:70,	     
    	    items:[
    	     	{
       			  layout:'column',
       			  xtype:'fieldset',
       			  title:'零件信息',
       			  items:[
       			  	{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  			  {
       			  		     xtype:'textfield',
							 name : 'code',
			                 fieldLabel:'零件编码',
			                 focusOrder:11
       			  		   	},{
       			  		     xtype:'textfield',
							 name : 'brand',
			                 fieldLabel:'品牌',
			                 focusOrder:21
       			  		   },{
       			  		   	xtype:'textfield',
       			  		   	name:'swapCode',
       			  		   	fieldLabel:'互换码',
       			  		   	focusOrder:31,
							allowBlank: true
       			  		   }
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  		  sourceCombo,{
       			  			   xtype:'textfield',
       			  			   name:'carModel',
       			  			   fieldLabel:'车型'	,
       			  			   allowBlank:true,
       			  			   focusOrder:22
       			  			},{
       			  	    	  xtype:'textfield',				    									
					          name : 'measurementUnit',
			                  fieldLabel:'单位',
			                  readOnly:true,
			                  style:{background:'#dddddd'}
       			  	       }
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[ {
       			  			   xtype:'textfield',				    
							   allowBlank : true,
			                   name : 'name',
			                   fieldLabel:'零件名称',
			                   readOnly:true
       			  			},{
       			  				xtype:'textfield',
       			  				name:'origin',
       			  				allowBlank : true,
       			  				fieldLabel:'产地'
       			  			}
       			  		]
       			  	}
       			  ]
    		}]
		});
		return this.tab_partProperty_form;
	},
	funGetTabInventoryForm:function(){
		this.tab_inventory_form=new Ext.FormPanel({
			frame:true,
    		border:false,
    		height:00,
    		labelAlign:'right',
    		labelWidth:70,	     
    	    items:[
    	    {
       			  layout:'column',
       			  xtype:'fieldset',
       			  title:'库存信息',
       			  items:[
       			  	{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  			  {
       			  				xtype:'numberfield',
       			  				name:'minOnHand',
       			  				allowNegative: false,
       			  				focusOrder:41,
       			  				fieldLabel:'下限库存数'
       			  			},{
       			  		   	xtype:'numberfield',
       			  		   	name:'onWay',
       			  		   	fieldLabel:'在途数',
							allowBlank: true,
							readOnly:true,
							style:{background:'#dddddd'}
							}
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  		  {
       			  				xtype:'numberfield',
       			  				name:'maxOnHand',
       			  				allowNegative: false,
       			  				focusOrder:42,
       			  				fieldLabel:'上限库存数'
       			  			},{
       			  				xtype:'textfield',
       			  				name:'cost', 
       			  				readOnly:true,
       			  				fieldLabel:'成本价'
       			  			}
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[ {
       			  		   	xtype:'textfield',
       			  		   	name:'onHand',
       			  		   	fieldLabel:'库存数',
       			  		   	readOnly:true,
							allowBlank: true
       			  		   }
       			  		]
       			  	}
       			  ]
    		}
    	     	]
		});
		return this.tab_inventory_form;
	},
	funGetTabQuotationForm:function(){
		var publicanLevelCombo=Uec3.App.dictionary.getCombo("partproperty_publicanLevel",{
			hiddenName : 'publicanLevel', 
			fieldLabel:'公开级别',
			mode : 'local',
			focusOrder:61
			});
		publicanLevelCombo.getStore().load();
		this.tab_quotation_form=new Ext.FormPanel({
			frame:true,
    		border:false,
    		height:100,
    		labelAlign:'right',
    		labelWidth:70,	     
    	    items:[
    	    { layout:'column',
       			  xtype:'fieldset',
       			  title:'报价信息',
       			  items:[
       			  	{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  			  publicanLevelCombo,{
       			  			   xtype:'numberfield',
							   name : 'minPrice',
							   fieldLabel:'最低价',
							   allowBlank: false,
            				   allowNegative: false,
            				   focusOrder:71
       			  			}
       			  			  
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[
       			  		 {
       			  		     xtype:'numberfield',
							 name : 'retailPrice',
							 fieldLabel:'媒体价',
            				 allowNegative: false
       			  		   },{
       			  		   	xtype:'textfield',
       			  		   	name:'quotationQty',
       			  		   	fieldLabel:'上架数'
       			  		   }
       			  		]
       			  	},{
       			  		columnWidth:.33,
       			  		layout:'form',
       			  		defaults:{anchor:'99%',style:{'background':'#dddddd'}},
       			  		items:[ {
       			  		   	 xtype:'numberfield',
							 name : 'insurancePrice',
							 fieldLabel:'保险价',
                             allowNegative: false
       			  		   },{
       			  		   	xtype:'hidden',
       			  		   	name:'id'
       			  		   },{
       			  		   	xtype:"hidden",
       			  		   	name:'partId'
       			  		   }
       			  		]
       			  	}
       			  ]
    		}
    	     	]
		});
		return this.tab_quotation_form;
	},
	funInventoryGridRowSelect:function(selectionModel,rowId,record){
		var _this=this;
		_this.ajax.request({
		    url :Uec3.App.Constants.homeUrl+"/part/query_partProperty!searchPartPropertyBySouAndCode.do",
		    params :{json:Ext.encode({partId:record.data.partId})},
		    method :"post",
		    success:function(response,options){	                	 
		    	  var result = Ext.decode(response.responseText);
		    	  if(result&&result.partProperty){
		    	      var partProperty=result.partProperty;
		    	      this.tab_partProperty_form.form.setValues(partProperty);
					  this.tab_inventory_form.form.setValues(partProperty);
					  this.tab_quotation_form.form.setValues(partProperty);
		    	  }else{
		    	  	  this.partPropertySearchForm.getForm().reset();
		    	      Ext.MessageBox.alert("提示","无此零件!");
		    	      Ext.MessageBox.hide.defer(600,Ext.MessageBox);
		    	  }
		    }.createDelegate(this),
		    failure:function(response,options){
		          Ext.MessageBox.alert("提示","数据加载异常");
		        }
		});
		
	},
	funFormLevel1CobmoSelectEvent:function(){
		var store=this.formLevel2Cobmo.getStore();
		store.baseParams={level1Id:this.formLevel1Cobmo.value};
		store.load();
	},
	show:function(){
		Uec3.InventoryWindow.superclass.show.call(this);
		
	}
});

	
Uec3.InventoryModule=Ext.extend(Ext.app.Module, {
	id : 'inventory',
	createWindow : function(params) {
		var desktop = Uec3.App.getDesktop();
		var module=this;
		var win = desktop.getWindow('inventory');
		if (!win) {
			win = desktop.createWindow({
				id : 'inventory',
				title : '库存浏览',
				width : document.body.clientWidth * 0.9,
				height : document.body.clientHeight * 0.9,
				iconCls : 'acc-icon',
				params:params||{},
				shim : false,
				//autoScroll:true,
				module:module,
				animCollapse : false,
				constrainHeader : true,
				maximizable : true,
				taskbuttonTooltip : "库存浏览",
				layout : "border"
			},Uec3.InventoryWindow);
			
		}
		win.show();

	}
});
